package org.kpli.jak.net.first;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class InfoServer {

	private final int INFO_PORT = 50010;

	private String datafromClient;

	BufferedReader inFromClient;

	DataOutputStream outToClient;

	public InfoServer() {

		Socket serverSocket;
		try {
			ServerSocket infoServer = new ServerSocket(INFO_PORT);
			System.out.println("Step 1 : Server telah siap...");
			while (true) {
				serverSocket = infoServer.accept();
				System.out.println("Step 2 : Ada client " + "yang terkoneksi!");
				inFromClient = new BufferedReader(new InputStreamReader(
						serverSocket.getInputStream()));
				outToClient = new DataOutputStream(
						serverSocket.getOutputStream());
				boolean isQUIT = false;
				while (!isQUIT) {

					if (datafromClient.startsWith("QUIT")) {
						isQUIT = true;
					}
				}
				outToClient.close();
				inFromClient.close();
				serverSocket.close();
				System.out.println("Step 3 : Koneksi client tertutup..");
			}
		} catch (IOException ioe) {
			System.out.print("error: " + ioe);
		} catch (Exception e) {
			System.out.print("error: " + e);
		}
	}

	public void closed() {

	}

	/* program utama */
	public static void main(String[] args) {
		new InfoServer();
	}
}
